# make a list of numbers between two limits
min = 10
max = 15
list(range(min, max))[10, 11, 12, 13, 14]
For today, a very brief one that crops up surprisingly often. As a reminder, I’ll post one tiny example per day with the intention that they should only take a couple of minutes to read.
If you want to read them all but can’t be bothered checking this website each day, sign up for the mailing list:
and I’ll send a single email at the end with links to them all.
This seems like a silly example, but it’s surprising how often beginners run into it - using one of Python’s built-in function names as a variable name:
[10, 11, 12, 13, 14]
In many programming languages this is not allowed, and would cause an error, but in Python the above code is perfectly legal. It doesn’t cause a problem until later in the program where we want to use the max function for its intended purpose:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[2], line 1 ----> 1 max([23,76,34,91]) TypeError: 'int' object is not callable
Although this problem is very simply explained, there are a few things that can make it hard to fix for beginners.
Firstly, the error message does not make it particularly obvious what has happened.
Secondly, the place in the code where the error occurs can be a long way away from the place in the code where the function is overwritten.
Thirdly, if we are working in a notebook or other interactive environment, deleting or changing the line
max = 15
will not actually fix the problem, as the overwritten version will still be in Python’s memory. We need to restart the kernel for the fix to take effect. The ability to preserve state like this is both the great strength and the great weakness of notebooks!
Bonus: if we really don’t want to restart our interactive environment - say we have the results of some long-running calculation in memory that we don’t want to lose - a quick fix is to use the del keyword to remove our overwritten variable name:
and restore the original.
One more time; if you want to see the rest of these little write-ups, sign up for the mailing list: